/**
 * Copyright (c) Software Institude of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.transformation.editor.controller.editparts.node;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editpolicies.DirectEditPolicy;

import edu.pku.sei.modeler.edit.parts.AbstractNodeEditPart;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.controller.policies.NodeComponentEditPolicy;
import edu.pku.sei.transformation.editor.controller.policies.TransformationNodeEditPolicy;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.constraint.Constraint;
import edu.pku.sei.transformation.editor.views.figures.ConstraintFigure;

/**
 * @author fengchao
 * 
 */
public class ConstraintEditpart extends AbstractNodeEditPart {

	public ConstraintEditpart(MetaShapeElement model) {
		super(model);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.pku.sei.modeler.edit.parts.BaseEditPart#createDirectEditPolicy()
	 */
	@Override
	protected DirectEditPolicy createDirectEditPolicy() {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	@Override
	protected IFigure createFigure() {
		ConstraintFigure figure = new ConstraintFigure();
		Constraint c = (Constraint) getNode().getModelElement();
		figure.setHead("<<Constraint>>");
		figure.setText(c.getContent());
		return figure;
	}

	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE,
				new TransformationNodeEditPolicy());
		installEditPolicy(EditPolicy.COMPONENT_ROLE,
				new NodeComponentEditPolicy());
	}

	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		ConstraintFigure figure = (ConstraintFigure) getFigure();
		if (ModelPackage.CONSTRAINT_CONTENT.equals(propertyName)) {
			figure.setText(event.getNewValue().toString());
		} else if (ModelPackage.CONSTRAINT_STATE.equals(propertyName)) {
			int state = (Integer) event.getNewValue();
			if (state == Constraint.STATE_SUCCESS) {
				figure.setSuccessIcon();
			} else if (state == Constraint.STATE_FAIL) {
				figure.setFailedIcon();
			} else {
				figure.clearIcon();
			}
		} else {
			super.handlePropertyChanged(event);
		}
	}

}
